﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;

public partial class ProductsAll : System.Web.UI.Page
{
    private int nCurrentLang = Support.GetCurrentLanguage() - 1;
    private int nDefaultLang = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            this.BindData();
        }
    }

    private void BindData()
    {
        lblProducts.Text = MultipleLanguage.PRODUCT[nCurrentLang];
        lblTitle.Text = MultipleLanguage.PRODUCT[nCurrentLang];
        lblMap.Text = MultipleLanguage.PRODUCT[nCurrentLang];
        Products objProducts = new Products();
        DataTable dtbProducts = new DataTable();
        dtbProducts = objProducts.GetAll();
        rptProducts.DataSource = dtbProducts;
        //Search(nItemView, "", sProductName, nItemID, sParent, nTrademark, nMaterial, dPriceFrom, dPriceTo, isSaleOff, BestSeller, nStatus, PercentSaleOff, ExpriedDateSaleOff, StartedDateSaleOff, nDefaultLang, nCurrentLang, 1, Config.PAGESIZE, ref nPageCount);
        rptProducts.DataBind();
    }


    protected void rptProducts_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAILS + "?" + Config.PRO_ID+  "=" + nID;
                Response.Redirect(sDetail);
                break;

        }
    }
    protected void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string sCode = string.Empty;
        string sPrice = string.Empty;
        string sTrademark = string.Empty;
        string sDescript = string.Empty;
        sPrice = MultipleLanguage.PRICE[nCurrentLang] + ": ";
        sCode = MultipleLanguage.PROCODE1[nCurrentLang];
        sTrademark = MultipleLanguage.TRADEMARK[nCurrentLang] + ": ";
        sDescript = MultipleLanguage.DESCRIPTION[nCurrentLang] + ": ";

        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            HyperLink lnkProductName = (HyperLink)e.Item.FindControl("lnkProductName");
            Image imgproducs = (Image)e.Item.FindControl("imgproducs");
            Label lblDescript = (Label)e.Item.FindControl("lblDescript");
            Label lblDescripttitle = (Label)e.Item.FindControl("lblDescripttitle");
            Label lblCode = (Label)e.Item.FindControl("lblCode");
            Label lblCodetitle = (Label)e.Item.FindControl("lblCodetitle");
            Label lblPrice = (Label)e.Item.FindControl("lblPrice");
            Label lblPricetitle = (Label)e.Item.FindControl("lblPricetitle");
            Label lblTrademark = (Label)e.Item.FindControl("lblTrademark");
            Label lblTrademarktitle = (Label)e.Item.FindControl("lblTrademarktitle");

            HyperLink lnkimgProduct = (HyperLink)e.Item.FindControl("lnkimgProduct");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            string sTitle = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName1"));
            
            double dPrice = ConvertData.ConvertToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
            lnkimgProduct.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAILS + "?" + Config.PAGE_ID + "=" + nID;
            lnkProductName.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAILS + "?" + Config.PAGE_ID + "=" + nID;



            string sToolTip = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName1"));

            if (sToolTip.Length > 0) { imgproducs.ToolTip = sToolTip; }
            else { imgproducs.ToolTip = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MainContent2")); }

            if (sTitle.Length > 0) { lnkProductName.Text = sTitle;}
            else {lnkProductName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName2"));}

          
            if (dPrice > 0) {  }
            if (dPrice > 0)
            { lblPrice.Text = ConvertData.ConvertToString(Support.FormatCurrency(DataBinder.Eval(e.Item.DataItem, "Price"))) + " " + "₫"; }
            else
            {
                lblPrice.Text =  MultipleLanguage.PRICE_CALL[nCurrentLang];
            }
            lblDescripttitle.Text = sDescript;
            lblPricetitle.Text = sPrice;
            lblCodetitle.Text = sCode;
            lblCode.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode"));
            lblTrademark.Text = sTrademark + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode"));
            imgproducs.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ImageDefault"));
            lblTrademarktitle.Text = sTrademark;
            lblTrademark.Text =  " " + GetTrademark(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "TradeMarkID")));
            string sDescription = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "MainContent1"));
            if (sDescription.Length > 130)
            {
                sDescription = ConvertData.TruncateString(sDescription, 100) + Config.DOT;
            }
            lblDescript.Text = sDescription;
            //btnImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));
        }
    }

    private string GetTrademark(int nID)
    {
        ProductTradeMark obj = new ProductTradeMark();
        obj.LoadById(nID);
        string sReturn = obj.Data.TradeMarkName;
        return sReturn;
    }
}